package car.aistar.util;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName XrbUtil
 * @Description TODO 时间差距（获取天）
 * @Author laoxu
 * @Date 2020/10/3 10:03
 * Version 1.0
 **/

public class XrbUtil {


    public static Long getDay(String date){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long days = null;

        try {
            Date currentTime = dateFormat.parse(dateFormat.format(new Date()));
            Date pastTime = dateFormat.parse(date);
            Long diffDay = currentTime.getTime() - pastTime.getTime();
            days = diffDay / (1000*60*60*24);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }

    /**
    *@Author laoxu
    *@Description //TODO 字符串类型转时间类型
    *@Date 10:33 2020/10/3
    *@Param [str]
    *@return java.util.Date
    **/
    public static Date StringToDate(String str) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(str);
            return simpleDateFormat.parse(str);
    }
    
    /**
    *@Author laoxu
    *@Description //TODO 日期类型转字符串类型
    *@Date 10:58 2020/10/3
    *@Param [date]
    *@return java.lang.String
    **/
    public static String DateToString(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatDate = dateFormat.format(date);
        return formatDate;
    }
    
    /**
    *@Author laoxu
    *@Description //TODO 前一个月时间
    *@Date 12:48 2020/10/3
    *@Param []
    *@return java.util.Date
    **/
    public static Date getOneMonth(){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        return m;
    }

    /**
    *@Author laoxu
    *@Description //TODO 前七天
    *@Date 12:49 2020/10/3
    *@Param
    *@return
    **/
    public static Date sevenDay(){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, - 7);
        Date d = c.getTime();
        return d;
    }

    /**
    *@Author laoxu
    *@Description //TODO 过去三个月
    *@Date 12:52 2020/10/3
    *@Param
    *@return
    **/
    public static Date threeMonth(){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -3);
        Date m3 = c.getTime();
        return m3;
    }

    /**
    *@Author laoxu
    *@Description //TODO 过去一年
    *@Date 12:52 2020/10/3
    *@Param []
    *@return java.util.Date
    **/
    public static Date oneYear(){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        return y;
    }


        /**
         * 将PageInfo对象泛型中的Po对象转化为Vo对象
         * @param pageInfoPo PageInfo<Po>对象</>
         * @param <P> Po类型
         * @param <V> Vo类型
         * @return
         */
        public static <P, V> PageInfo<V> PageInfo2PageInfoVo(PageInfo<P> pageInfoPo) {
            // 创建Page对象，实际上是一个ArrayList类型的集合
            Page<V> page = new Page<>(pageInfoPo.getPageNum(), pageInfoPo.getPageSize());
            page.setTotal(pageInfoPo.getTotal());
            return new PageInfo<>(page);
        }

}
